/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.services;

import com.ez2find.manager.SiteManagerLocal;
import com.ez2find.model.Area;
import com.ez2find.model.Community;
import com.ez2find.model.Event;
import com.ez2find.model.Forum;
import com.ez2find.model.Gig;
import com.ez2find.model.Hous;
import com.ez2find.model.Job;
import com.ez2find.model.Message;
import com.ez2find.model.Personal;
import com.ez2find.model.RegisterUser;
import com.ez2find.model.Resume;
import com.ez2find.model.Sale;
import com.ez2find.model.Service;
import com.ez2find.model.Site;
import com.ez2find.model.Topic;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Owner
 */
@Stateful
public class SessionContextBean implements SessionContextLocal {
    
    @EJB
    private SiteManagerLocal ejbSiteManager;
    private RegisterUser owner;
    private Site currentSite;
    private Area currentArea;
    private Job job;
    private Resume resume;
    private Hous hous;
    private Sale sale;
    private Gig gig;
    private Service service;
    private Community community;
    private Personal personal;
    private Event event;
    private Forum currentForum;
    private Topic currentTopic;
    private Message first;
    private Message currentReply;
    private Boolean reply;
    
    

    public RegisterUser getOwner() {
        return owner;
    }

    public void setOwner(RegisterUser owner) {
        this.owner = owner;
    }

    public Site getCurrentSite() {
        if(currentSite==null){
            FacesContext fcontext = FacesContext.getCurrentInstance();
            Cookie[] cookies = ((HttpServletRequest)fcontext.getExternalContext().getRequest()).getCookies();
            if(cookies != null && cookies.length > 0){
                for(int i=0; i<cookies.length;i++){
                    Cookie c = cookies[i];
                    if(c.getName().equals("ez2findCurrentSite")){
                        currentSite=ejbSiteManager.find(Long.parseLong(c.getValue()));
                        return currentSite;
                    }
                }
            }
            Cookie cookieCurrentSite = new Cookie("ez2findCurrentSite", "32");
            cookieCurrentSite.setMaxAge(31536000);
            cookieCurrentSite.setPath("/");
            ((HttpServletResponse)fcontext.getExternalContext().getResponse()).addCookie(cookieCurrentSite);
            currentSite=ejbSiteManager.find(32);
        }
        return currentSite;
    }

    public void setCurrentSite(Site site) {
        FacesContext fcontext = FacesContext.getCurrentInstance();
        Cookie[] cookies = ((HttpServletRequest)fcontext.getExternalContext().getRequest()).getCookies();
        if(cookies != null && cookies.length > 0){
            for(int i=0; i<cookies.length;i++){
                Cookie c = cookies[i];
                if(c.getName().equals("ez2findCurrentSite")) {
                    c.setValue(String.valueOf(site.getId()));
                    c.setMaxAge(31536000);
                    c.setPath("/");
                    ((HttpServletResponse)fcontext.getExternalContext().getResponse()).addCookie(c);
                    break;
                }
            }
        }
        this.currentSite = site;
        currentArea=null;
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public Area getCurrentArea() {
        return currentArea;
    }

    public void setCurrentArea(Area currentArea) {
        this.currentArea = currentArea;
    }

    public Resume getResume() {
        return resume;
    }

    public void setResume(Resume resume) {
        this.resume = resume;
    }

    public Hous getHous() {
        return hous;
    }

    public void setHous(Hous hous) {
        this.hous = hous;
    }

    public Sale getSale() {
        return sale;
    }

    public void setSale(Sale sale) {
        this.sale = sale;
    }

    public Gig getGig() {
        return gig;
    }

    public void setGig(Gig gig) {
        this.gig = gig;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public Community getCommunity() {
        return community;
    }

    public void setCommunity(Community community) {
        this.community = community;
    }

    public Personal getPersonal() {
        return personal;
    }

    public void setPersonal(Personal personal) {
        this.personal = personal;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
    
    public Forum getCurrentForum() {
        return currentForum;
    }

    public void setCurrentForum(Forum currentForum) {
        this.currentForum = currentForum;
    }

    public Topic getCurrentTopic() {
        return currentTopic;
    }

    public void setCurrentTopic(Topic currentTopic) {
        this.currentTopic = currentTopic;
    }

    public Message getFirst() {
        return first;
    }

    public void setFirst(Message first) {
        this.first = first;
    }

    public Message getCurrentReply() {
        return currentReply;
    }

    public void setCurrentReply(Message currentReply) {
        this.currentReply = currentReply;
    }

    public Boolean getReply() {
        return reply;
    }

    public void setReply(Boolean reply) {
        this.reply = reply;
    }
}
